package com.sinosoft.web.easyui.api;

import cn.com.sinux.spring.commons.PropertyCollection;
import cn.com.sinux.spring.json.JsonFilter;
import cn.com.sinux.spring.utils.PageUtils;
import com.sinosoft.hydra.um.condition.SearchBestProcessCondition;
import com.sinosoft.hydra.um.condition.SearchBestRecordCondition;
import com.sinosoft.hydra.um.constant.BaseConstant;
import com.sinosoft.hydra.um.domain.permission.TBestProcess;
import com.sinosoft.hydra.um.domain.permission.TBestRecord;
import com.sinosoft.hydra.um.services.ProcessManageService;
import com.sinosoft.hydra.um.services.SysBestService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 最优流程和最优参数相关api
 */
@Controller
@RequestMapping("/api/best")
public class SysBestApi {

    @Autowired
    private SysBestService sysBestService;
    @Autowired
    private ProcessManageService processManageService;

    /**
     * 新增最优流程
     *
     * @param model 返回模型
     * @return 返回新增页面
     */
    @RequestMapping("toAddProcess")
    public String toAddProcess(Model model) {
        model.addAttribute("groups", processManageService.findAllProcess());
        return "hydra_easyui/best/addProcess";
    }

    /**
     * 修改最优流程
     *
     * @param model 返回模型
     * @param id 最优流程id
     * @return 流程修改页面
     */
    @RequestMapping("toUpdateProcess")
    public String toAddProcess(Model model, @RequestParam(value = "id", required = true) String id) {
        model.addAttribute("process", sysBestService.findOneBestProcess(id));
        model.addAttribute("groups", processManageService.findAllProcess());
        return "hydra_easyui/best/updateProcess";
    }


    /**
     * 最优流程首页面
     *
     * @return 参数页面
     */
    @RequestMapping("process")
    @RequiresPermissions(value = "best:process:list")
    public String process() {
        return "hydra_easyui/best/process";
    }


    /**
     * 最优参数首页面
     *
     * @return 参数页面
     */
    @RequestMapping("record")
    @RequiresPermissions(value = "best:record:list")
    public String record() {
        return "hydra_easyui/best/record";
    }

    /**
     * 新增最优参数
     *
     * @param model 返回模型
     * @return 新增页面
     */
    @RequestMapping("toAddRecord")
    @RequiresPermissions(value = "best:record:add")
    public String toAddRecord(Model model) {
        //查找所有工艺模板
        model.addAttribute("groups", processManageService.findAllProcess());
        return "hydra_easyui/best/addRecord";
    }

    /**
     * 新增最优流程
     *
     * @param bestRecord 最优参数表单项
     * @return 保存结果
     */
    @RequestMapping(value = "addRecord", method = RequestMethod.POST)
    @RequiresPermissions(value = "best:record:add")
    @ResponseBody
    public String addRecord(TBestRecord bestRecord) {
        //查找所有工艺模板
        TBestRecord result = sysBestService.saveBestRecord(bestRecord, BaseConstant.SaveType.SAVE_TYPE_ADD);
        return JsonFilter.doPropertyFilter(result, "id");
    }

    /**
     * 修改最优流程
     *
     * @param bestRecord 最优参数表单项
     * @return 保存结果
     */
    @RequestMapping("updateRecord")
    @ResponseBody
    @RequiresPermissions(value = "best:record:update")
    public String updateRecord(TBestRecord bestRecord) {
        //查找所有工艺模板
        TBestRecord result = sysBestService.saveBestRecord(bestRecord, BaseConstant.SaveType.SAVE_TYPE_UPDATE);
        return JsonFilter.doPropertyFilter(result, "id");
    }

    /**
     * 删除最优流程项
     *
     * @param searchBestProcessCondition ids集合
     * @return 删除结果
     */
    @RequestMapping(value = "processDelete", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = "best:data:delete")
    public String bestDelete(SearchBestProcessCondition searchBestProcessCondition) {
        List<TBestProcess> bestProcesses = sysBestService.deleteBestProcess(searchBestProcessCondition.getIds());
        return JsonFilter.doPropertyFilter(bestProcesses, new PropertyCollection()
                .addProperties(TBestProcess.class, "id"));
    }


    /**
     * 新增最优流程
     *
     * @param form 流程表单项
     * @return 保存结果
     */
    @RequestMapping(value = "addProcess", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = "best:process:add")
    public Object addProcess(TBestProcess form) {
        TBestProcess bestProcess = sysBestService.saveBestProcess(form, BaseConstant.SaveType.SAVE_TYPE_ADD);
        return JsonFilter.doPropertyFilter(bestProcess, new PropertyCollection()
                .addProperties(TBestProcess.class, "id"));
    }

    /**
     * 修改最优流程
     *
     * @param form 流程表单项
     * @return 保存结果
     */
    @RequestMapping(value = "updateProcess", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = "best:process:update")
    public Object updateProcess(TBestProcess form) {
        TBestProcess bestProcess = sysBestService.saveBestProcess(form, BaseConstant.SaveType.SAVE_TYPE_UPDATE);
        return JsonFilter.doPropertyFilter(bestProcess, new PropertyCollection()
                .addProperties(TBestProcess.class, "id"));
    }

    /**
     * 查询最优流程分页对象
     *
     * @param searchBestProcessCondition 查询条件
     * @param pageNumber 分页
     * @param pageSize 单页条数
     * @return 分页对象
     */
    @RequestMapping("processList")
    @ResponseBody
    public Object bestList(SearchBestProcessCondition searchBestProcessCondition,
                           @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNumber,
                           @RequestParam(value = "rows", required = false, defaultValue = "10") Integer pageSize) {
        Pageable pageable = PageUtils
                .buildPageRequest(pageNumber, pageSize, "id", "desc");
        Page<TBestProcess> bestProcesses = sysBestService.findBestProcess(searchBestProcessCondition, pageable);
        return JsonFilter.doPropertyFilter(bestProcesses, new PropertyCollection()
                .addProperties(TBestProcess.class, "id", "title", "reason", "remark", "createTime"));
    }

    /**
     * 查询最优参数分页对象
     *
     * @param searchBestRecordCondition 查询条件
     * @param pageNumber 分页
     * @param pageSize 单页条数
     * @return 分页对象
     */
    @RequestMapping("recordList")
    @ResponseBody
    public Object bestList(SearchBestRecordCondition searchBestRecordCondition,
                           @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNumber,
                           @RequestParam(value = "rows", required = false, defaultValue = "10") Integer pageSize) {
        Pageable pageable = PageUtils
                .buildPageRequest(pageNumber, pageSize, "id", "desc");
        Page<TBestRecord> bestProcesses = sysBestService.findBestRecord(searchBestRecordCondition, pageable);
        return JsonFilter.doPropertyFilter(bestProcesses, new PropertyCollection()
                .addProperties(TBestProcess.class, "id", "title", "reason", "remark", "createTime"));
    }

    /**
     * 删除最优参数
     *
     * @param searchBestRecordCondition 删除条件
     * @param forceDelete 是否强制删除
     * @return 删除结果
     */
    @RequestMapping("recordDelete")
    @ResponseBody
    @RequiresPermissions(value = "best:record:delete")
    public Object bestProcess(SearchBestRecordCondition searchBestRecordCondition,
                              @RequestParam(value = "forceDelete", required = false) boolean forceDelete) {
        List<TBestRecord> records = sysBestService.recordDelete(searchBestRecordCondition.getIds(), forceDelete);
        return JsonFilter.doPropertyFilter(records, new PropertyCollection()
                .addProperties(TBestRecord.class, "id"));
    }

    /**
     * 最优流程参数详情
     *
     * @param id 流程id
     * @return 详情
     */
    @RequestMapping("processBestRecords")
    @ResponseBody
    @RequiresPermissions(value = "best:record:list")
    public Object processBestRecords(@RequestParam(value = "id", required = true) String id) {
        List<TBestRecord> records = sysBestService.processBestRecords(id);
        return JsonFilter.doPropertyFilter(records, new PropertyCollection());
    }

    /**
     * 查询最优流程
     *
     * @param model 返回模型
     * @param id 流程主键
     * @return 页面
     */
    @RequestMapping("bestProcess")
    @RequiresPermissions(value = "best:process:list")
    public String bestProcess(Model model, @RequestParam(value = "id", required = true) String id) {
        model.addAttribute("best", sysBestService.findOneBestProcess(id));
        return "hydra_easyui/best/bestProcess";
    }


    /**
     * 搜索最优流程
     *
     * @return 页面
     */
    @RequestMapping("searchProcess")
    public String searchProcess() {
        return "hydra_easyui/best/searchProcess";
    }


    /**
     * 搜索最优参数
     *
     * @return 页面
     */
    @RequestMapping("searchRecord")
    public String searchRecord() {
        return "hydra_easyui/best/searchRecord";
    }

}
